package contegor.scire.structure.queries;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import contegor.scire.core.Connectable;
import contegor.scire.core.Id;
import contegor.scire.core.NeedsPrecedent;
import contegor.scire.core.TaskId;
import contegor.scire.core.Version;
import contegor.scire.core.WithSubsequentsExtensible;
import contegor.scire.core.queries.Query;

/**
 * {@link Query} used to do recursion in a {@link RecursiveQuery}.
 *
 * @version $Revision$
 * @author  Héctor Cirbián Pousa ($Author$)
 */
@Version("$Id$")
public abstract class CircularQuery<T extends Connectable> extends Query<T> implements WithSubsequentsExtensible<T, T>, NeedsPrecedent<T, T> {
  private static final long serialVersionUID = -7380334202818129200L;

  protected List<NeedsPrecedent<? super T, T>> subsequents;

  protected CircularQuery() {
    super(null, null);
  }

  protected CircularQuery(TaskId taskId, Id containerId) {
    super(taskId, containerId);
  }

  public void setTaskId(TaskId taskId) {
    this.taskId = taskId;
  }

  public TaskId getTaskId() {
    return this.taskId;
  }

  public void setTargetContainerId(Id targetContainerId) {
    this.targetContainerId = targetContainerId;
  }

  public Id getTargetContainerId() {
    return this.targetContainerId;
  }

  @Override
  public CircularQuery<T> withIt(NeedsPrecedent<? super T, T> subsequent) {
    if (this.subsequents == null)
      this.subsequents = new LinkedList<NeedsPrecedent<? super T,T>>();
    this.subsequents.add(subsequent);
    return this;
  }

  @Override
  public List<NeedsPrecedent<? super T, T>> getSubsequentTasks() {
    if (this.subsequents == null)
      return Collections.emptyList();
    return this.subsequents;
  }
}
